package org.lagou.Dao.impl;

import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.lagou.Dao.CompanyDao;
import org.lagou.bean.CompanyBean;

public class CompanyDaoImpl extends BaseDao implements CompanyDao {

	@Override
	public List<CompanyBean> getCompanyAll() {
		try {
			return query.query("select * from company", new BeanListHandler<CompanyBean>(CompanyBean.class));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<CompanyBean> getCompanyPagin(int pageIndex, int pageSize) {
		try {
			return query.query("select * from company limit ?,?", new BeanListHandler<CompanyBean>(CompanyBean.class),
					(pageIndex - 1) * pageSize, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public long getCompanyCount() {
		try {
			return query.query("select count(1) from company", new ScalarHandler<Long>());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public List<CompanyBean> getCompanyById(int id) {
		try {
			return query.query("select * from company where id=?", new BeanListHandler<CompanyBean>(CompanyBean.class),
					id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<CompanyBean> getCompanyByUserid(int userid) {
		try {
			return query.query("select * from company where userid=?",
					new BeanListHandler<CompanyBean>(CompanyBean.class), userid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int addCompany(CompanyBean info) {
		try {
			return query.update(
					"insert into company(userid,name,abbreviation,logo,email,url,address,field,scale,stage,lightspot,tag,genertor,position,sina,summary,img,poster,product,producturl,productsummary,introduce,date,type) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
					info.getUserid(), info.getName(), info.getAbbreviation(), info.getLogo(), info.getEmail(),
					info.getUrl(), info.getAddress(), info.getField(), info.getScale(), info.getStage(),
					info.getLightspot(), info.getTag(), info.getGenerator(), info.getPosition(), info.getSina(),
					info.getSummary(), info.getImg(), info.getPoster(), info.getProduct(), info.getProducturl(),
					info.getProductsummary(), info.getIntroduce(), info.getDate(), info.getType());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateCompany(CompanyBean info) {
		try {
			return query.update(
					"update company set userid=?,name=?,abbreviation=?,logo=?,email=?,url=?,address=?,field=?,scale=?,stage=?,lightspot=?,tag=?,genertor=?,position=?,sina=?,summary=?,img=?,poster=?,product=?,producturl=?,productsummary=?,introduce=?,date=?,type=? where id=?",
					info.getUserid(), info.getName(), info.getAbbreviation(), info.getLogo(), info.getEmail(),
					info.getUrl(), info.getAddress(), info.getField(), info.getScale(), info.getStage(),
					info.getLightspot(), info.getTag(), info.getGenerator(), info.getPosition(), info.getSina(),
					info.getSummary(), info.getImg(), info.getPoster(), info.getProduct(), info.getProducturl(),
					info.getProductsummary(), info.getIntroduce(), info.getDate(), info.getType(), info.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int deleteCompany(int id) {
		try {
			return query.update("delete from company where id=?", id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateTypeById(CompanyBean info) {
		try {
			return query.update("update company set type=? where id=?", info.getType(), info.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public List<CompanyBean> getCompanyByName(String name) {
		try {
			return query.query("select * from company where company.name like ?",  new BeanListHandler<CompanyBean>(CompanyBean.class),"%"+name+"%");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public int addCompanyInformation(CompanyBean info) {
		try {
			return query.update(
					"insert into company(userid,name,abbreviation,logo,email,url,address,field,scale,stage,lightspot) values(?,?,?,?,?,?,?,?,?,?,?)",
					info.getUserid(), info.getName(), info.getAbbreviation(), info.getLogo(), info.getEmail(),
					info.getUrl(), info.getAddress(), info.getField(), info.getScale(), info.getStage(),
					info.getLightspot());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}
	
	@Override
	public int updateCompanylogo(CompanyBean info) {
		try {
			return query.update("update company set logo=? where userid=?", info.getLogo(), info.getUserid());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}


	@Override
	public int updateCompanyInformation(CompanyBean info) {
		try {
			return query.update(
					"update company set name=?,abbreviation=?,logo=?,email=?,url=?,address=?,field=?,scale=?,stage=?,lightspot=? where userid=?",
					info.getName(), info.getAbbreviation(), info.getLogo(), info.getEmail(), info.getUrl(),
					info.getAddress(), info.getField(), info.getScale(), info.getStage(), info.getLightspot(),
					info.getUserid());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateCompanyTag(CompanyBean info) {
		try {
			return query.update("update company set tag=? where userid=?", info.getTag(), info.getUserid());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateGeneratorInformation(CompanyBean info) {
		try {
			return query.update("update company set generator=?,position=?,sina=?,summary=? where userid=?", info.getGenerator(),
					info.getPosition(), info.getSina(),info.getSummary(), info.getUserid());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateGeneratorImg(CompanyBean info,int userid) {
		try {
			return query.update("update company set img=? where userid=?", info.getImg(), userid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updatePoster(CompanyBean info) {
		try {
			return query.update("update company set poster=?,date=? where userid=?", info.getPoster(),info.getDate(), info.getUserid());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateProductInformation(CompanyBean info) {
		try {
			return query.update("update company set product=?,producturl=?,productsummary=?,date=?,type=? where userid=?", info.getProduct(),
					info.getProducturl(), info.getProductsummary(),info.getDate(),info.getType(), info.getUserid());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateCompanIintroduce(CompanyBean info) {
		try {
			return query.update("update company set introduce=?,date=?,type=? where userid=?", info.getIntroduce(),
					info.getDate(), info.getType(), info.getUserid());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}
}
